package com.zdsoft.crm.controller.auth;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.zdsoft.crm.entity.auth.SysUserRole;
import com.zdsoft.crm.service.auth.ISysRoleService;
import com.zdsoft.crm.service.auth.ISysUserRoleService;
import com.zdsoft.crm.service.auth.ISysUserService;

@Controller
@RequestMapping("/auth/user_role")
public class UserRoleController {

	@Autowired
	ISysUserService userService;
	
	@Autowired
	ISysRoleService roleService;
	
	@Autowired
	ISysUserRoleService userRoleService;
	
	@GetMapping("/{id}")
	public ModelAndView index(@PathVariable("id") String id) {
		ModelAndView mav = new ModelAndView("auth/user_role/form.html","user",userService.findById(id));
		mav.addObject("roleList", roleService.findAll());
		return mav;
	}

	@PostMapping("/{id}")
	public String save(@PathVariable("id") String id,String roleId) {
		SysUserRole userRole = new SysUserRole();
		userRole.setId(UUID.randomUUID().toString().replace("-", ""));
		userRole.setRole_id(roleId);
		userRole.setUser_id(id);
		userRoleService.save(userRole);
		return "redirect:/auth/user/list";
	}
}
